﻿//var personnelmanagement = angular.module("personnelmanagement", ['restangular', 'vModal', 'xuanzeqitk', 'btnconqitk', 'checklist-model', 'personnelLabel', 'PensonSelector', 'tm.pagination', 'cgBusy'])


// 添加人员
personnelmanagement.controller('addUserModal', ['$scope', 'addUserFactory', 'Msg', '$location', '$state', 'RequestPopService', 'requestStatisticService', 'MsgCon', 'sysUserInfo', '$rootScope', function ($scope, addUserFactory, Msg, $location, $state, RS, RSS, MsgCon, sysUserInfo, $rootScope) {
    var abc = this;
    abc.closeuser = addUserFactory.deactivate;
    $scope.mytext = "";
    $scope.myfujian = []; //获得到附件列表
    $scope.quanxianjson = []; //当前用户选择的权限数据
    $scope.currentTab = 0;
    $rootScope.btnTab = "index.personnelmanagement";
    $scope.btnlist = { savefun: false, addinfo: false, editinfo: false, delinfo: false };
    $scope.showOtherInfo = false; //默认关闭附加属性
    $scope.nowdate = "";
    $scope.needSaveImg = false; //不需要保存图片

    $scope.showOther = function () {
        if ($scope.showOtherInfo) {
            $scope.nowdate = document.getElementById("birthDate").value;
        }
        $scope.showOtherInfo = !$scope.showOtherInfo;
    };
    //人员基本信息
    $scope.userinfo = { user_ID: "", user_No: "", user_Account: "", user_Pwd: "", state: "0", powerLV: "99", user_Name: "", email: "", phone: "", user_Img: "", createUserId: "", createUserName: "", updateUserId: "", updateUserName: "", startTime: "", endTime: "", startDate: "", endDate: "", identifyCard: "", user_Sex: null };

    //人员备注日志信息
    $scope.loginfo = { logId: "", logText: "", userId: "", updateUserId: "", updateUserName: "", loginfoshow: "false" };

    //人员附加信息
    $scope.userinfo.other = { id: "", account_QQ: "", account_weChat: "", education: "", nation: "", graduate_school: "", birthDate: "", operate_userid: sysUserInfo.getuserinfo().user_ID, userid: "", username: "", orgid: sysUserInfo.getuserinfo().organization_ID, Telephone: "", political: "4", duties: "", servants: "0", usertype: "" };

    //通信任务
    $scope.comtask = { organization_ID: sysUserInfo.getuserinfo().organization_ID, comtask_name: "录入用户", oper_id: sysUserInfo.getuserinfo().user_ID, oper_name: sysUserInfo.getuserinfo().username, comtask_porper: 0, comtask_emailnum: 0, comtask_phonenum: 0, comtask_inemailnum: 0 };

    //通信详细
    $scope.cominfo = { serviceStatus: 1, recipientId: "", recipientName: "", recipientMail: "", recipientPhone: "", sendTitle: "新增用户通知", sendContent: "用户基本信息", sendMode: 1, comtaskId: "" };
    $scope.cominfolist = [];

    /******/
    //用户状态
    $scope.userStart = { "divclass": "newlyaddejgrjsjzzt", "showflag": false, "state": $scope.userinfo.state, objinfo: [], optype: "2", "divulclass": "newlyaddedjszt", "ddicon": "", "ddstyle": { "margin-top": "0px"} };
    $scope.userStart.objinfo = [{ "ddname": "正常", "ddvalue": "0", "ddicon": "&#xe69d;" }, { "ddname": "冻结", "ddvalue": "1", "ddicon": "&#xe69d;"}];
    //系统角色
    $scope.sysRole = { "divclass": "newlyaddedxtjsstudent", "showflag": false, "state": $scope.userinfo.powerLV, objinfo: [], optype: "1", "divulclass": "newlyaddedjszt", "ddicon": "", "ddstyle": { "margin-top": "0px"} };
    $scope.sysRole.objinfo = [{ "ddname": "管理员", "ddvalue": "2", "ddicon": "&#xe632;" }, { "ddname": "教师", "ddvalue": "3", "ddicon": "&#xe603;" }, { "ddname": "学员", "ddvalue": "99", "ddicon": "&#xe6b5;"}];

    /******/

    $scope.userLogList = [];
    $scope.mysubmit = function () {
        //        console.log($scope.mytext);
        //console.log($scope.myfujian);        
        if ($scope.userinfo.user_ID != "" && $scope.mytext.length > 0) {

            $scope.objinfo.logText = $scope.mytext;
            var date = new Date();
            var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
            var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
            var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
            $scope.objinfo.logCreateTime = date.getFullYear() + "-" + date.getMonth() + "-" + date.getDate() + " " + hours + ":" + minute + ":" + second;

            $scope.loginfo.userId = $scope.title;
            $scope.loginfo.logText = encodeURI($scope.mytext);
            $scope.loginfo.updateUserId = JSON.parse(localStorage.getItem("userinfo")).userId;
            $scope.loginfo.updateUserName = encodeURI(JSON.parse(localStorage.getItem("userinfo")).username);
            $scope.userinfo.logText = $scope.mytext;
            $scope.userinfo.logCreateTime = $scope.objinfo.logCreateTime;

            if ($scope.loginfo.logId != "") {
                $scope.promise = RS.personnelLogUpdate({ data: $scope.loginfo }).then(function (retobj) {

                    if (retobj.errorcode == 0) {
                        Msg.success('保存成功！');
                        //                        $scope.loggetlist();
                    } else {
                        //console.log(retobj.errorcode);
                    }
                }, function errorCallback() {
                    //                    console.log("连接服务器失败！");
                    Msg.error('连接服务器失败！');
                });
            } else {
                $scope.promise = RS.personnelLogAdd({ data: $scope.loginfo }).then(function (retobj) {
                    if (retobj.errorcode == 0) {
                        Msg.success('保存成功！');
                        $scope.loggetlist();
                        $scope.mytext = "";
                    } else {
                        //console.log(retobj.errorcode);
                    }
                }, function errorCallback() {
                    //                    console.log("连接服务器失败！");
                    Msg.error('连接服务器失败！');
                });
            }
        } else {
            if ($scope.userinfo.user_ID == "" && $scope.mytext.length > 0) {
                Msg.warning('信息储存请点击确定保存信息！');
                //if ($scope.userLogList.length > 0) {
                $scope.userLogList[0] = { "logText": $scope.mytext };
                //                } else {
                //                    $scope.userLogList.push({ "logText": $scope.mytext });
                //                }
            } else {
                Msg.warning('备注信息不能为空！');
            }
        }

    }
    $scope.getlistparam = {
        userId: $scope.title,
        pageIndex: "1",
        pageSize: "5"
    }
    // 加载更多的五条记录
    $scope.isGetListpage = true;
    $scope.nextlog = function () {
        $scope.isGetListpage = false;
        $scope.getlistparam.pageIndex = parseInt($scope.getlistparam.pageIndex) + 1;
        $scope.loggetlist();
    }
    $scope.PerLogList = [];
    $scope.loggetlist = function () {
        if ($scope.isGetListpage) { // 执行了 增、删、改
            $scope.getlistparam.pageIndex = 1;
            $scope.getlistparam.pageSize = $scope.PerLogList.length == 0 || $scope.PerLogList.length < 5 ? 5 : $scope.PerLogList.length; // 防止开始时候空对象
        }
        $scope.promise = RS.personnelLogGetList($scope.getlistparam).then(function (retobj) {
            if ($scope.isGetListpage) {
                $scope.PerLogList = retobj.datas
            } else {
                Array.prototype.push.apply($scope.PerLogList, retobj.datas);
            }
            $scope.isGetListpage = true;    // 控制点击的是 true删除、添加、编辑 false 重新赋值
            //            console.log("日志列表：");
            //            console.log(retobj.datas);
        }, function errorCallback() {
            //        console.log("连接服务器失败！");
            Msg.error('连接服务器失败！');
        });
    }

    $scope.userstatestr = [{ stateval: "1", statestr: "正常" }, { stateval: "0", statestr: "冻结"}];
    $scope.userslivestr = ["学员", "教师", "管理员"];

    //声明唯一部门对象
    $scope.PublicObj = {
        organization_ID: "",
        organization_Name: "",
        parentId: "",
        parentName: "",
        organization_Num: 0,
        organization_Sort: "",
        organization_No: "",
        orgOutsidePhone: "",
        orgHousePhone: "",
        Phone: ""
    };
    //由于人员选择器设计多选 在此处调用需要进行数据独立
    $scope.OnlyOne = function (userOrgList) {
        if (userOrgList.length > 1) {
            userOrgList.splice(0, 1);
        }
    }
    //获取选择器所选对象
    $scope.GetSelectModal = function (userOrgList, obj) {
        try {
            if (userOrgList.length > 0) {
                return obj = userOrgList[0];
            }
        } catch (e) {
            //console.log(e);
        }
    }

    //左侧选择器初始化
    $scope.ApiData = {
        windowModal: "1", //调用模式 1弹窗模式 2宽度大于450 3 宽度小于450
        ViceOrgOpen: false, //HtmlOrg弹框样式 关闭 选择器再次引用选择器样式
        ViceGroupOpen: false, //HtmlOrg弹框样式 关闭 选择器再次引用选择器样式
        ViceRoleOpen: false, //HtmlOrg弹框样式 关闭 选择器再次引用选择器样式
        ApiOpen: false, //为true时 页面打开选择器 false 关闭控制器
        menu: [//进入人员选择器的菜单

         {"id": "Org" }
         ],
        "ViceuserRoleList": [//传入的角色列表 再次点击 查询 双向绑定不能影响数据源

             ],
        "ViceuserOrgList": [//传入的部门列表 再次点击 查询 双向绑定不能影响数据源

                ],
        "ViceuserGroupList": [//传入的用户组 再次点击 查询 双向绑定不能影响数据源

             ],
        userOrgList: [],
        userRightList: []
    };

    //页面监听部门左侧列表选项
    $scope.watch = function () {
        $scope.$watch('ApiData.userOrgList', function (newValue, oldValue, scope) {
            $scope.OnlyOne($scope.ApiData.userOrgList); //排除集合对象 
            $scope.PublicObj = $scope.GetSelectModal($scope.ApiData.userOrgList, $scope.PublicObj); //取单一

        }, true);
    }

    //    console.log($scope.title + "_1");
    //打开部门、角色、用户组
    $scope.UpdateParentFun = function (num, event) {
        switch (num) {
            case 1: //部门
                $scope.ParentData.menu = [{ "id": "Org"}];
                break;
            case 2: //角色
                $scope.ParentData.menu = [{ "id": "Role"}];
                break;
            case 3: //用户组
                $scope.ParentData.menu = [{ "id": "Group"}];
                break;
        }
        //        menu: [{ "id": "Org"}],
        $scope.ParentData.ApiOpen = true;
        $scope.ParentData.event = event;

    }

    $scope.ssssss = function () {
        $scope.stateflag = false;
    }

    // 选择器初始化
    $scope.ParentData = {
        ApiOpen: false,
        menu: [{ "id": "Org"}],
        userOrgList: [],
        userGroupList: [],
        userRoleList: [],
        userAuthorityList: [],
        userOrgValues: [],
        userGroupValues: [],
        userRightValues: []
    }
    // 权限id
    $scope.authority_ID = "";

    //删除选中的标签（1组织机构、3用户组、2角色、）
    $scope.deltag = function (tagid, typeid) {
        //        $scope.ApiData.ViceuserOrgList.splice(result, 1);
        var resultIndex = 0;
        if (typeid == 1) {
            resultIndex = $scope.containsIndex($scope.ParentData.userOrgList, tagid, typeid);
            $scope.ParentData.userOrgList.splice(resultIndex, 1);
            resultIndex = $scope.containsIndex($scope.ParentData.userOrgValues, tagid, typeid);
            $scope.ParentData.userOrgValues.splice(resultIndex, 1);
        }
        else if (typeid == 2) {
            resultIndex = $scope.containsIndex($scope.ParentData.userRoleList, tagid, typeid);
            $scope.ParentData.userRoleList.splice(resultIndex, 1);
            resultIndex = $scope.containsIndex($scope.ParentData.userRightValues, tagid, typeid);
            $scope.ParentData.userRightValues.splice(resultIndex, 1);
        }
        else if (typeid == 3) {
            resultIndex = $scope.containsIndex($scope.ParentData.userGroupList, tagid, typeid);
            $scope.ParentData.userGroupList.splice(resultIndex, 1);
            resultIndex = $scope.containsIndex($scope.ParentData.userGroupValues, tagid, typeid);
            $scope.ParentData.userGroupValues.splice(resultIndex, 1);
        }
        //        console.log(tagid + "_" + typeid + "_" + resultIndex);
    }

    //判断 字符在数组的索引
    $scope.containsIndex = function (arr, objid, typeid) {
        try {
            var i = arr.length;
            while (i--) {
                if (typeid == 1) {
                    if (arr[i].organization_ID === objid) {
                        return i;
                    }
                } else if (typeid == 2) {
                    if (arr[i].roles_ID === objid) {
                        return i;
                    }
                } else if (typeid == 3) {
                    if (arr[i].userGroup_ID === objid) {
                        return i;
                    }
                }
            }
            return -1;
        } catch (e) {
            // statements
            return -1;
        }
    }

    if ($scope.title != "") {
        $scope.loggetlist();
    }
    //编辑
    if ($scope.title != "") {
        $scope.promise = RS.personnelGetKey({ user_ID: $scope.title }).then(function (retobj) {
            if (retobj.errorcode == 0) {



                $scope.userinfo = angular.copy(retobj.data);

                //给附加属性赋值
                if ($scope.userinfo.userContact) {
                    $scope.userinfo.other = $scope.userinfo.userContact;
                } else {
                    //人员附加信息
                    $scope.userinfo.other = { id: "", account_QQ: "", account_weChat: "", education: "", nation: "", graduate_school: "", birthDate: "", operate_userid: sysUserInfo.getuserinfo().user_ID, userid: "", username: "", orgid: sysUserInfo.getuserinfo().organization_ID, Telephone: "" };
                }


                if (retobj.data.startTime == undefined) { $scope.userinfo.startTime = ""; }
                if (retobj.data.endTime == undefined) { $scope.userinfo.endTime = ""; }
                if (retobj.data.startDate == undefined) { $scope.userinfo.startDate = ""; }
                if (retobj.data.startTime == undefined) { $scope.userinfo.endDate = ""; }

                $scope.ParentData.userOrgList = retobj.data.userOrgList;
                $scope.ParentData.userRoleList = retobj.data.userRoleList;
                $scope.ParentData.userGroupList = retobj.data.userGroupList;

                $scope.userStart.state = $scope.userinfo.state;
                $scope.sysRole.state = $scope.userinfo.powerLV;

                var i = $scope.ParentData.userOrgList.length;
                while (i--) {
                    $scope.ParentData.userOrgValues.push({ organization_ID: $scope.ParentData.userOrgList[i].organization_ID, boss: $scope.ParentData.userOrgList[i].boss });
                }

                var ii = $scope.ParentData.userRoleList.length;
                while (ii--) {
                    $scope.ParentData.userRightValues.push({ roles_ID: $scope.ParentData.userRoleList[ii].roles_ID, boss: $scope.ParentData.userRoleList[ii].boss });
                }

                var iii = $scope.ParentData.userGroupList.length;
                while (iii--) {
                    $scope.ParentData.userGroupValues.push({ userGroup_ID: $scope.ParentData.userGroupList[iii].userGroup_ID, boss: $scope.ParentData.userGroupList[iii].boss });
                }

            } else {
                //console.log(retobj.errorcode);
                //                 状态码:0 成功1 失败 5连接不上数据库 6 系统内部错误
            }
            //            $scope.loaddata();
        }, function errorCallback() {
            Msg.error('连接服务器失败！');
            //            console.log("连接服务器失败！");
            //声明唯一部门对象

            /*
            $scope.userinfo = { user_ID: "", user_No: "", user_Account: "", user_Pwd: "", state: "", powerLV: "", user_Name: "", email: "", phone: "", user_Img: "", createUserId: "", createUserName: "", updateUserId: "", updateUserName: "", startTime: "", endTime: "" };        
            $scope.ParentData.userOrgList = [{}];
            $scope.ParentData.userRoleList = [{}];
            $scope.ParentData.userGroupList = [{}];
            */
        });
        $scope.promise = RS.GetAuthority({ bqx_xid: $scope.title }).then(function (Data) {
            if (Data.errorcode == "0") {
                if (Data.data != null) {
                    var quanxianstr = Data.data.bqx_quanxian.split(",");
                    $scope.authority_ID = Data.data.bqx_Id;
                    $scope.ParentData.userAuthorityList = quanxianstr;
                } else {
                    $scope.ParentData.userAuthorityList = [];
                }
            } else if (Data.errorcode != "0") {
            }
        }, function callback() {
            Msg.error("请求数据有误");
            $scope.promise = 0;
        });
    }
    // 关闭用户编辑、新增页面
    $scope.closeuser = function () { $scope.title = ""; addUserFactory.deactivate(); }
    // 0正常 1锁定
    $scope.updsate = function (stateval) {
        if ($scope.stateflag) { $scope.stateflag = false; } else { $scope.stateflag = true; }
        if (stateval != 2) {
            $scope.userinfo.state = stateval;
        }
    }
    // 1超级管理员、2普通管理员、3教师、99学员
    $scope.updroles = function (rolesval) {
        if ($scope.rolesflag) { $scope.rolesflag = false; } else { $scope.rolesflag = true; }
        if (rolesval != 1) {
            $scope.userinfo.powerLV = rolesval;
        }
    }
    //自动生成编号
    $scope.Newuser_No = function () {
        //        var guid = new GUID();
        //        $scope.userinfo.user_No = guid.newGUID();

        var date = new Date();
        var seperator1 = "";
        var seperator2 = "";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate
                    + "" + date.getHours() + seperator2 + date.getMinutes()
                    + seperator2 + date.getSeconds() + date.getMilliseconds();
        $scope.userinfo.user_No = currentdate;
    }

    //打开功能权限窗口
    $scope.addgongnengchuangkou = function () {
        $scope.ParentData2.ApiOpen2 = true;
    };
    if ($scope.userinfo.user_No == undefined || $scope.userinfo.user_No.length == 0) {
        $scope.Newuser_No();
    }
    //保存用户头像
    $scope.SaveFun = function () {
        $scope.addinfo();
    }
    $scope.isBtnLoading = false;
    //保存用户信息
    $scope.addinfo = function () {

        if ($scope.userinfo.user_Account == undefined || $scope.userinfo.user_Account.length == 0) { Msg.warning('登录帐号不能为空！'); return; }
        if ($scope.userinfo.user_Pwd == undefined || $scope.userinfo.user_Pwd.length == 0) {
            if ($scope.userinfo.user_ID != undefined && $scope.userinfo.user_ID.length <= 0) {
                Msg.warning('登录密码不能为空！');
                return;
            }
        }

        var regNumber = /\d+/; //验证0-9的任意数字最少出现1次。
        var regString = /[a-z]+/; //验证小写26个字母任意字母最少出现1次。
        var regString2 = /[A-Z]+/; //验证大写26个字母任意字母最少出现1次。

        if ($scope.userinfo.user_Pwd != undefined) {

        
            if ($scope.userinfo.user_Pwd.length > 16) {
                Msg.warning('登录密码不能大于16位！'); return;
            }
            if ($scope.userinfo.user_Pwd.length < 6) {
                Msg.warning('登录密码不能小于6位！'); return;
            }
            if (!regNumber.test($scope.userinfo.user_Pwd)) {
                Msg.warning('登录密码中必须含有数字！'); return;
            }
            if (!regString.test($scope.userinfo.user_Pwd)) {
                Msg.warning('登录密码中必须含有小写字母！'); return;
            }
            if (!regString2.test($scope.userinfo.user_Pwd)) {
                Msg.warning('登录密码中必须含有大写字母！'); return;
            }
        }
        //        if ($scope.userinfo.user_No == undefined || $scope.userinfo.user_No.length == 0) { Msg.warning('人员编号不能为空！'); return; }
        if ($scope.userinfo.user_Name == undefined || $scope.userinfo.user_Name == 0) { Msg.warning('人员名称不能为空！'); return; }

        //if ($scope.userinfo.email == undefined || $scope.userinfo.email == 0) { Msg.warning('联系邮箱不能为空！'); return; }
        //if ($scope.userinfo.phone == undefined || $scope.userinfo.phone == 0) { Msg.warning('手机号码不能为空！'); return; }
        $scope.isBtnLoading = true; // 按钮loading

        if ($scope.userinfo.organization_ID == undefined || $scope.userinfo.organization_ID == null || $scope.userinfo.organization_ID.length <= 0) {
            if (sysUserInfo.getuserinfo().organization_ID != undefined && sysUserInfo.getuserinfo().organization_ID.length > 0) {
                $scope.userinfo.organization_ID = sysUserInfo.getuserinfo().organization_ID;
            }
            if (sysUserInfo.getuserinfo().organization_Name != undefined && sysUserInfo.getuserinfo().organization_Name.length > 0) {
                $scope.userinfo.organization_Name = sysUserInfo.getuserinfo().organization_Name;
            }
            $scope.userinfo.createUserId = JSON.parse(localStorage.getItem("userinfo")).userId;
            $scope.userinfo.createUserName = JSON.parse(localStorage.getItem("userinfo")).username;
        }
        //----------------------------------------------------------我是华丽的分割线--------------------------------------------------------------



        var userOrgList = $scope.ParentData.userOrgValues; //$scope.ParentData.userOrgList;
        var userRoleList = $scope.ParentData.userRightValues; //  $scope.ParentData.userRoleList;
        var userGroupList = $scope.ParentData.userGroupValues; // $scope.ParentData.userGroupList;
        if ($scope.userinfo.hasOwnProperty("userOrgList")) {
            delete $scope.userinfo.userOrgList;
        }
        if ($scope.userinfo.hasOwnProperty("userRoleList")) {
            delete $scope.userinfo.userRoleList;
        }
        if ($scope.userinfo.hasOwnProperty("userGroupList")) {
            delete $scope.userinfo.userGroupList;
        }
        var userAuthorityList = [];
        var userLogListnew = null;  //日志添加
        if ($scope.userinfo.user_ID == "" && $scope.userLogList.length == 0) { if ($scope.mytext) { userLogListnew = { "logText": $scope.mytext }; } } else {
            userLogListnew = $scope.userLogList[0];
        }

        $scope.userinfo.startTime = document.getElementById("startTime").value;
        $scope.userinfo.endTime = document.getElementById("endTime").value;
        $scope.userinfo.startDate = document.getElementById("startDate").value;
        $scope.userinfo.endDate = document.getElementById("endDate").value;

        //附加属性
        //        if ($scope.sysRole.state != 99) {
        if ($scope.nowdate == null || $scope.nowdate.length <= 0) {
            if (document.getElementById("birthDate") && document.getElementById("birthDate").value.length > 0) {
                $scope.nowdate = document.getElementById("birthDate").value;
            } else {
                $scope.nowdate = null;
            }
        }
        $scope.userinfo.other.birthDate = $scope.nowdate;

        //$scope.userinfo.other.Telephone = $scope.userinfo.phone;
        $scope.userinfo.other.userid = $scope.userinfo.user_ID;
        //        }
        $scope.userinfo.state = $scope.userStart.state;
        $scope.userinfo.powerLV = $scope.sysRole.state;
        if ($scope.userinfo.user_Img != undefined && $scope.userinfo.user_Img.indexOf("data:") != -1) {
            $scope.resUrl = $scope.userinfo.user_Img;
            $scope.userinfo.user_Img = "-1";
        }
        $scope.userinfo.modifyUserId = sysUserInfo.getuserinfo().user_ID;
        $scope.userinfo.modifyUse_Name = sysUserInfo.getuserinfo().user_Name;

        $scope.promise = RS.personnelAddEdit({ loginUserId: sysUserInfo.getuserinfo().user_ID, data: JSON.stringify($scope.userinfo), userOrgList: userOrgList, userRoleList: userRoleList, userGroupList: userGroupList, userAuthorityList: userAuthorityList, userLogList: userLogListnew, authority_ID: $scope.authority_ID, authority: $scope.ParentData.userAuthorityList.join(","), sendFlag: $scope.sendFlag, contact: JSON.stringify($scope.userinfo.other), organizationId: sysUserInfo.getuserinfo().orgfrist, organizationName: sysUserInfo.getuserinfo().orgfristname }).then(function (data) {
            //JSON.parse(localStorage.getItem("userinfo")).userId
            $scope.isBtnLoading = false; // 按钮loading
            if (data.errorcode == 0) {
                if (data.data != undefined) {
                    $scope.userinfo.user_ID = data.data.user_ID;
                }
                //有编辑图片操作
                if ($scope.needSaveImg) {
                    $scope.confile();
                }
                Msg.success('保存成功！');
                $scope.title = "";
                $scope.closeuser();

                //                //新增 保存成功后发送邮件
                //                if (data.data != undefined) {
                //                    $scope.comtask.comtask_porper = 1;
                //                    $scope.cominfo.recipientMail = $scope.userinfo.email;
                //                    $scope.cominfo.recipientPhone = $scope.userinfo.phone;
                //                    $scope.cominfo.recipientName = $scope.userinfo.user_Name;
                //                    $scope.cominfo.recipientId = $scope.userinfo.user_No;

                //                    $scope.cominfolist.push($scope.cominfo);

                //                    RS.AddInfoAndTask({ data: $scope.comtask, cominfoList: $scope.cominfolist }).then(function (retobj1) {

                //                        console.log(retobj1);
                //                    });

                //                }
                $scope.loginfo = { logId: "", logText: "", userId: "", updateUserId: "", updateUserName: "", loginfoshow: "false" };
                //                $scope.loginfo = { "logId": "", "logText": "", "userId": "", "updateUserId": "", "updateUserName": "", "loginfoshow": "false" };
                //跳转用户列表页面
                //$location.path("/personnelmanagement");
                $scope.ParentData.userAuthorityList = [];
                $state.go("index.personnelmanagement", { id: 0, routename: "index.personnelmanagement" }, { reload: true });
            } else if (data.errorcode == "38") {
                Msg.warning("警告", "企业人数超过最大限制！");
            } else if (data.errorcode == "27") {
                Msg.warning("用户登录帐号已存在！");
            } else {
                //console.log(data.errorcode);
                Msg.error('保存用户信息失败！');
            }
            $scope.userLogList = [];
            //errorcode true string 状态码:0 成功1 失败2账号已存在3账号格式不正确 5连接不上数据库 6 系统内部错误 24 参数有误
        }, function errorCallback() {
            $scope.title = "";
            Msg.error('连接服务器失败！');
            //$scope.loginerr = true;
            //$scope.loginerrmsg = "连接服务器失败！";
        });
    }
    //编辑 
    $scope.logedit = function (loginfo) {
        loginfo.loginfoshow = true;
        $scope.loginfo = loginfo;
        //$scope.mytext = loginfo.logText;        
    }

    //删除备注
    $scope.logdel = function (logId) {
        MsgCon.delnoicon().then(function (flag) {   // 删除弹框
            if (flag) {
                $scope.promise = RS.personnelLogDel({ logId: logId, userid: $scope.userinfo.user_ID }).then(function (retobj) {
                    if (retobj.errorcode == 0) {
                        Msg.success("删除成功！");
                        $scope.loggetlist();
                    } else {
                        //console.log(retobj.errorcode);
                    }
                }, function errorCallback() {
                    Msg.error('连接服务器失败！');
                });
            }
        });
        //        console.log(logId);
    }
    //保存
    $scope.logsave = function (logId) {
        logId.loginfoshow = false;
        //        console.log(logId);
        $scope.promise = RS.personnelLogUpdate({ data: logId }).then(function (retobj) {
            //            console.log(retobj);
            if (retobj.errorcode == 0) {
                Msg.success('保存成功！');
            }
        }, function errorCallback() {
            Msg.error('连接服务器失败！');
        });
        $scope.loginfo = { "logId": "", "logText": "", "userId": "", "updateUserId": "", "updateUserName": "", "loginfoshow": "false" };
    }
    //取消编辑
    $scope.logcancel = function (loginfo) {
        loginfo.loginfoshow = false;
        $scope.loginfo = { "logId": "", "logText": "", "userId": "", "updateUserId": "", "updateUserName": "", "loginfoshow": "false" };
        //        console.log(logId);
    }

    $scope.Bossflag = function (obj, flag) {
        obj.boss = flag;

        if (obj.userGroup_ID != undefined) {
            var i = $scope.ParentData.userGroupValues.length;
            while (i--) {
                if ($scope.ParentData.userGroupValues[i].userGroup_ID === obj.userGroup_ID) {
                    $scope.ParentData.userGroupValues[i].boss = flag;
                    break;
                }
            }
        } else if (obj.roles_ID != undefined) {
            var i = $scope.ParentData.userRightValues.length;
            while (i--) {
                if ($scope.ParentData.userRightValues[i].roles_ID === obj.roles_ID) {
                    $scope.ParentData.userRightValues[i].boss = flag;
                    break;
                }
            }
        } else {
            var i = $scope.ParentData.userOrgValues.length;
            while (i--) {
                if ($scope.ParentData.userOrgValues[i].organization_ID === obj.organization_ID) {
                    $scope.ParentData.userOrgValues[i].boss = flag;
                    break;
                }
            }
        }
    }
    //关闭人员头像框
    $scope.saveImgCode = function () {
        $scope.needSaveImg = true; //需要保存图片
        $scope.headShow = false;
    }
    // 打开人员头像截取
    $scope.headShow = false;
    $scope.imgUrl = "";
    $scope.resUrl = ""; //人员头像base64码
    $scope.openCropImg = function () {
        $scope.headShow = true;
        //console.log($scope.userinfo.user_Img);
        $scope.imgUrl = $scope.userinfo.user_Img;
    }
    /*****************************************************************************************************************/

    $scope.confile = function () {

        var params = { blob: $scope.resUrl, userid: $scope.userinfo.user_ID };
        RSS.uploadCover(params).success(function (data) {
            if (data.errorcode == 0) {
                Msg.success('上传头像成功！');
                $scope.userinfo.user_Img = data.errormsg + "?time=" + new Date();
            } else {
                Msg.error('请求失败！');
            }
            //响应成功操作
        }).error(function (data) {
            Msg.error('连接服务器失败！');
            //响应失败（响应以错误状态返回）操作
        })
    }

    /**  
    * 将以base64的图片url数据转换为Blob  
    * @param urlData  
    *            用url方式表示的base64图片数据  
    */
    $scope.getfileByBase64 = function convertBase64UrlToBlob(urlData) {

        var bytes = window.atob(urlData.split(',')[1]);        //去掉url的头，并转换为byte  

        //处理异常,将ascii码小于0的转换为大于0  
        var ab = new ArrayBuffer(bytes.length);
        var ia = new Uint8Array(ab);
        for (var i = 0; i < bytes.length; i++) {
            ia[i] = bytes.charCodeAt(i);
        }

        return new Blob([ab], { type: 'image/png' });
    }
    //*******************************************************2018-8-27***************************************************************/
    //政治面貌集合
    $scope.politicalList = [
        { name: "中共党员", value: "1" },
        { name: "共青团员", value: "2" },
        { name: "民主党派", value: "3" },
        { name: "群众", value: "4" }
    ];
    //用户类型集合
    $scope.usertypeList = [
        { name: "自定义", value: "自定义" },
        { name: "系统内", value: "系统内" },
        { name: "财政部", value: "财政部" },
        { name: "专员办", value: "专员办" },
        { name: "财政系统", value: "财政系统" }
    ];
    //切换性别
    $scope.checkedSexType = function (index) {
        $scope.userinfo.user_Sex = index;
    }
    //切换公务员
    $scope.checkedGWYType = function (index) {
        $scope.userinfo.other.servants = index;
    }





} ])
//融合账号
personnelmanagement.controller('rhUserModal', ['$scope', 'rhUserFactory', 'RequestPopService', 'Msg', 'MsgCon', 'sysUserInfo', '$rootScope', function ($scope, rhUserFactory, RPS, Msg, MsgCon, sysUserInfo, $rootScope) {

    //关闭
    $scope.onClose = function () {
        $scope.title = "";
        rhUserFactory.deactivate();

    }
    //未开始收搜
    $scope.sososhare = false,
    //未搜索到数据
    $scope.sharenull = false;
    //学员数据
    $scope.stuList = [];
    //查询学员数据
    $scope.params = {
        account: "",
        pwd: "",
        userid: sysUserInfo.getuserinfo().user_ID
    };
    $scope.code = "";
    //查询用户信息
    $scope.getstuInfo = function () {
        if ($scope.params.account.length <= 0) {
            Msg.warning("请输入账号！");
            return;
        }
        if ($scope.params.pwd.length <= 0) {
            Msg.warning("请输入密码！");
            return;
        }
        $scope.sososhare = true; //已经搜索过了
        RPS.findOrgUserInfo($scope.params).then(function (data) {
            if (data.errorcode == 0) {
                $scope.stuList = [];
                if (data.data != undefined && data.data != null) {
                    $scope.stuList.push(data.data);
                    $scope.code = data.code;
                }
            } else if (data.errorcode == 37) {
                Msg.warning("账号或密码错误！");
            } else if (data.errorcode == 34) {
                Msg.warning("非法操作！");
            } else if (data.errorcode == 39) {
                Msg.warning("无权限操作！");
            } else if (data.errorcode == 40) {
                Msg.warning("当前企业已经存在绑定关系，请勿重复绑定！");
            } else if (data.errorcode == 58) {
                Msg.warning("融合账号只能是学员！");
            } else {
                Msg.error('获取用户信息失败！');
            }
        }, function (data) {
            Msg.error('连接服务器失败！');
        })
    }
    //查询已融合的数据
    $scope.hadparam = {
        userid: sysUserInfo.getuserinfo().user_ID,
        searchText: "",
        searchType: 2,
        pageIndex: 1,
        pageSize: 10,
        pageCount: 0
    };
    $scope.hadlList = [];
    //查询企业已融合的用户
    $scope.getHadRh = function () {
        RPS.findOrgFusionRecord($scope.hadparam).then(function (data) {
            if (data.errorcode == 0) {

                if (data.datas.length > 0) {
                    data.datas.forEach(function (obj, index) {
                        obj.fusionTime = new Date(obj.fusionTime).format("yyyy-MM-dd hh-mm-ss");
                    });
                }
                if ($scope.hadparam.pageIndex == 1) {
                    $scope.hadlList = data.datas;
                } else {
                    $scope.hadlList = $scope.hadlList.concat(data.datas);
                }
                $scope.hadparam.pageCount = data.pageCount;
            } else {
                Msg.error('获取列表失败！');
            }
        }, function (data) {
            Msg.error('连接服务器失败！');
        })
    }
    $scope.loadMore = function () {
        $scope.hadparam.pageIndex += 1;
        $scope.getHadRh();
    }
    $scope.getHadRh();
    //融合的方法
    $scope.rhStu = function () {
        RPS.accountFusion({ userid: sysUserInfo.getuserinfo().user_ID, code: $scope.code }).then(function (data) {
            if (data.errorcode == 0) {
                Msg.success("融合成功！");
                $scope.stuList = [];
                $scope.hadparam.pageIndex = 1;
                $scope.getHadRh();
            } else {
                Msg.error('获取级别列表失败！');
            }
        }, function (data) {
            Msg.error('连接服务器失败！');
        })
    }

} ])

//超管融合账号
personnelmanagement.controller('upUserOrgModal', ['$scope', 'upUserOrgFactory', 'RequestPopService', 'Msg', 'MsgCon', 'sysUserInfo', '$rootScope', function ($scope, upUserOrgFactory, RPS, Msg, MsgCon, sysUserInfo, $rootScope) {
    //关闭
    $scope.onClose = function () {
        $scope.title = "";
        upUserOrgFactory.deactivate();

    }
    //未开始收搜
    $scope.sososhare = false,
    //未搜索到数据
    $scope.sharenull = false;
    //学员数据
    $scope.stuList = [];
    //查询学员数据
    $scope.params = {
        searchText: "",
        pageIndex: 1,
        pageSize: 10,
        userId: sysUserInfo.getuserinfo().user_ID
    };
    $scope.code = "";
    $scope.operatecode = "";
    //查询用户信息
    $scope.getorgInfo = function () {
        if ($scope.params.searchText.length <= 0) {
            Msg.warning("请输入企业名称！");
            return;
        }
        $scope.sososhare = true; //已经搜索过了
        RPS.findPlatform($scope.params).then(function (data) {
            if (data.errorcode == 0) {
                $scope.orgList = [];
                if (data.datas != undefined && data.datas != null && data.datas.length > 0) {
                    $scope.orgList = data.datas;
                    $scope.operatecode = data.code;
                    $scope.sharenull = false;
                } else {
                    $scope.sharenull = true;
                }
            } else if (data.errorcode == 24) {
                Msg.warning("无权限操作！");
            } else {
                Msg.error('获取用户信息失败！');
            }
        }, function (data) {
            Msg.error('连接服务器失败！');
        })
    }
    //查询已融合的数据
    $scope.hadparam = {
        operatorId: sysUserInfo.getuserinfo().user_ID,
        userid: $scope.user.user_ID,
        pageIndex: 1,
        pageSize: 10,
        pageCount: 0
    };
    $scope.hadlList = [];
    //查询企业已融合的用户
    $scope.getHadRh = function () {
        RPS.findUserOrg($scope.hadparam).then(function (data) {
            if (data.errorcode == 0) {
                $scope.code = data.code;
                if (data.datas.length > 0) {
                    data.datas.forEach(function (obj, index) {
                        obj.relationTime = new Date(obj.relationTime).format("yyyy-MM-dd hh-mm-ss");
                    });
                }
                if ($scope.hadparam.pageIndex == 1) {
                    $scope.hadlList = data.datas;
                } else {
                    $scope.hadlList = $scope.hadlList.concat(data.datas);
                }
                $scope.hadparam.pageCount = data.pageCount;
            } else if (data.errorcode == 27) {
                Msg.warning("融合账号只能是学员！");
            } else if (data.errorcode == 25) {
                Msg.warning("学员不存在！");
            } else if (data.errorcode == 24) {
                Msg.warning("无权限操作！");
            } else {
                Msg.error('获取列表失败！');
            }
        }, function (data) {
            Msg.error('连接服务器失败！');
        })
    }
    $scope.loadMore = function () {
        $scope.hadparam.pageIndex += 1;
        $scope.getHadRh();
    }
    $scope.getHadRh();
    //融合的方法
    $scope.addOrg = function (item) {
        RPS.platformFusion({ operatorCode: $scope.operatecode, code: $scope.code, orgId: item.organization_ID }).then(function (data) {
            if (data.errorcode == 0) {
                Msg.success("添加成功！");
                $scope.orgList.forEach(function (data, index) {
                    if (data.organization_ID == item.organization_ID) {
                        $scope.orgList.splice(index, 1);
                    }
                });
                $scope.hadparam.pageIndex = 1;
                $scope.getHadRh();
            } else if (data.errorcode == 26) {
                Msg.warning("企业id不合法！");
            } else if (data.errorcode == 25) {
                Msg.warning("code不合法！");
            } else if (data.errorcode == 28) {
                Msg.warning("当前用户已绑定该企业，请勿重复绑定！");
            } else if (data.errorcode == 58) {
                Msg.warning("融合账号只能是学员！");
            } else {
                Msg.error('获取列表失败！');
            }
        }, function (data) {
            Msg.error('连接服务器失败！');
        })
    }
    //删除的方法
    $scope.deleteOrg = function (orgid) {
        RPS.delAccountRelation({ operatorCode: sysUserInfo.getuserinfo().user_ID, code: $scope.code, orgId: orgid, type: 2 }).then(function (data) {
            if (data.errorcode == 0) {
                Msg.success("删除成功！");
                $scope.hadparam.pageIndex = 1;
                $scope.getHadRh();
            } else if (data.errorcode == 25) {
                Msg.warning("code不合法！");
            } else {
                Msg.error('获取列表失败！');
            }
        }, function (data) {
            Msg.error('连接服务器失败！');
        })
    }
} ])